﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cienco610.Models.DAL;
using Cienco610.Models;
using Cienco610.Models.Utils;

namespace Cienco610.Controllers
{
    public class AdvertController : Controller
    {
        //
        // GET: /Advert/
        private AdvertDAL advertDAL;
        private DBCiencoDataContext SC_DB;
        public AdvertController()
        {
            advertDAL = new AdvertDAL();
            SC_DB = new DBCiencoDataContext();
        }

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Test()
        {
            var listAdv = advertDAL.getListAdvert();
            return View(listAdv);
        }


        //
        // GET: /Advert/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Advert/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Advert/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Advert adv = new Advert();
                adv.AdvertName = collection["advertname"];
                adv.Description = collection["description"];
                adv.Link = collection["url"];
                String priority = collection["priority"];
                try
                {
                    adv.Priority = int.Parse(priority.Trim());
                }
                catch
                {
                    adv.Priority = 1;
                }
                var show = collection["show"];
                if (show == "false")
                {
                    adv.Show = false;
                }
                else
                {
                    adv.Show = true;
                }
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\ImageProjectWork");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion upload images
                adv.Image = uploader.PostedFile.FileName;
                SC_DB.CreateAdvert(adv.AdvertName, adv.Description, adv.Image, adv.Link, adv.Priority, adv.Show);
                return RedirectToAction("Test");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Advert/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Advert/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Advert/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Advert/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
